package org.oos.auth;

import java.io.IOException;
import java.util.List;

import org.oos.atom.AtomService.HttpStatusException;
import org.oos.dao.options.OptionsMap;

import com.google.api.client.http.GenericUrl;
import com.google.api.client.http.HttpContent;
import com.google.api.client.http.HttpRequest;
import com.google.api.client.http.HttpResponse;
import com.google.api.client.http.HttpResponseException;
import com.google.api.client.http.HttpTransport;

public class AnonymousAuthentication extends AbstractAuthentication {

	private static final long serialVersionUID = 1L;
	private String appToken;

	public String getAppToken() {
		return appToken;
	}

	public AnonymousAuthentication(String appToken) {
		this.appToken = appToken;
	}

	@Override
	public HttpResponse getResponse(HttpTransport transport, GenericUrl url,
			String method, HttpContent content, List<OptionsMap> parameters) throws IOException, HttpStatusException {
		try {
			HttpRequest request = transport.buildRequest();
			request.method = method;
			request.headers.put("Authorization", "app-token");
			request.headers.put("appToken", appToken);
			request.url = pack(url, parameters);
			request.content = content;
			HttpResponse response = request.execute();
			return response;
		} catch (HttpResponseException e) {
			HttpStatusException exception = new HttpStatusException(e.response.statusCode, "URL: " + url);
			exception.setResponse(e.response);
			throw exception;
		}
	}

	@Override
	public boolean isAnonymous() {
		return true;
	}
}
